﻿#region Copyright Info

// 
// Majesty of Omega - The next evolution of macro management space strategy
// 
// Copyright 2007-2009 André Claaßen
// 
// This file is protected by the terms and conditions of the
// Microsoft Community License (Ms-CL 1.1), a copy of which should
// have been distributed along with this software. If not,
// you may find the license information at the following URL:
// 
// http://www.microsoft.com/resources/sharedsource/licensingbasics/communitylicense.mspx

#endregion

#region

using System;
using System.Xml.Serialization;
using DesignByContract;

#endregion

namespace Majesty_of_Omega.Config
{
    /// <summary>
    /// Funding level of the research
    /// </summary>
    public enum FundingLevel
    {
        /// <summary>
        /// Cancel the research. At the next turn, all research of this item will be stopped
        /// </summary>
        Cancel,
        /// <summary>
        /// 25 % of research costs.
        /// </summary>
        OneQuarter,
        /// <summary>
        /// 50% of research 
        /// </summary>
        Half,
        /// <summary>
        /// 75% of resarch
        /// </summary>
        ThreeQuarter,
        /// <summary>
        /// 100% of research
        /// </summary>
        Full,
        /// <summary>
        /// 125% of resarch, with the risk of overruns.
        /// </summary>
        Crash
    }

    /// <remarks/>
    [XmlType(AnonymousType = true, Namespace = "http://majesty-of-omega/ResearchConfiguration.xsd")]
    [XmlRoot(Namespace = "http://majesty-of-omega/ResearchConfiguration.xsd", IsNullable = false)]
    public class ResearchConfiguration : Spreadsheet
    {
        /// <remarks/>
        [XmlArrayItem("Funding", IsNullable = false)]
        public ResearchFundingLevelInfo[] FundingLevels { get; set; }

        /// <remarks/>
        [XmlArrayItem("ProjOverEntry", IsNullable = false)]
        public ResearchConfigurationProjOverEntry[] ProjectOverrun { get; set; }

        /// <remarks/>
        [XmlArrayItem("OverAmountEntry", IsNullable = false)]
        public ResearchConfigurationOverAmountEntry[] OverrunAmount { get; set; }

        /// <summary>
        /// Gets or sets the research defaults.
        /// </summary>
        /// <value>The research defaults.</value>
        public ResearchConfigurationResearchDefaults ResearchDefaults { get; set; }

        /// <summary>
        /// Gets the funding level.
        /// </summary>
        /// <param name="level">The level.</param>
        /// <returns></returns>
        public ResearchFundingLevelInfo GetFundingLevelInfo(FundingLevel level)
        {
            return FundingLevels[Convert.ToInt32(level)];
        }

        /// <summary>
        /// Calculates a random progress in percent per turn
        /// </summary>
        /// <returns></returns>
        public int GetRandomAppliedResearchPercentagePerTurn()
        {
            Random random = new Random();
            return random.Next(ResearchDefaults.PercentagePerTurnMin, ResearchDefaults.PercentagePerTurnMax);
        }
    }

    /// <remarks/>
    [XmlType(AnonymousType = true, Namespace = "http://majesty-of-omega/ResearchConfiguration.xsd")]
    public class ResearchFundingLevelInfo
    {
        /// <remarks/>
        [XmlAttribute("Id")]
        public FundingLevel FundingLevel { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public string Unicode { get; set; }

        /// <summary>
        /// Gets the label.
        /// </summary>
        /// <value>The label.</value>
        public string Label
        {
            get { return StringManager.GetString(Unicode); }
        }

        /// <summary>
        /// Percentage of this funding level. Crash funding is greater then 100 percentage
        /// </summary>
        private int _percentage;

        /// <summary>
        /// Percentage of this funding level. Crash funding is greater then 100 percentage
        /// </summary>
        [XmlAttribute]
        public int Percentage
        {
            get { return _percentage; }
            set
            {
                Require.IsTrue(value >= 0 && value <= 125, "The percentage for a research funding must be greater then 0 and lesser equal 125 percent");
                _percentage = value;
            }
        }
    }

    /// <remarks/>
    [Serializable]
    [XmlType(AnonymousType = true, Namespace = "http://majesty-of-omega/ResearchConfiguration.xsd")]
    public class ResearchConfigurationProjOverEntry
    {
        /// <remarks/>
        [XmlAttribute]
        public byte Percentage { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public decimal ChancePercent { get; set; }
    }

    /// <remarks/>
    [Serializable]
    [XmlType(AnonymousType = true, Namespace = "http://majesty-of-omega/ResearchConfiguration.xsd")]
    public class ResearchConfigurationOverAmountEntry
    {
        /// <remarks/>
        [XmlAttribute]
        public byte Percentage { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public byte StartPercent { get; set; }

        /// <remarks/>
        [XmlAttribute]
        public byte EndPercent { get; set; }
    }


    /// <remarks/>
    [Serializable]
    [XmlType(AnonymousType = true, Namespace = "http://majesty-of-omega/ResearchConfiguration.xsd")]
    public class ResearchConfigurationResearchDefaults
    {
        /// <remarks/>
        public double PrimaryFieldCost { get; set; }

        /// <remarks/>
        public double SecondaryFieldCost { get; set; }

        /// <summary>
        /// Minimum progress in percentage for an applied research project
        /// </summary>
        public int PercentagePerTurnMin { get; set; }

        /// <summary>
        /// Maximum progress in percentage for an applied research project
        /// </summary>
        public int PercentagePerTurnMax { get; set; }

        private int _percentForFundamental;


        /// <summary>
        /// Gets or sets the percentage for fundamental research of the ai.
        /// The value must be in the  range between 1 and 99
        /// </summary>
        /// <value>The percent for fundamental.</value>
        public int PercentForFundamental
        {
            get
            {
                return _percentForFundamental;
            }
            set
            {
                Require.IsTrue(value >= 1 && value <= 99);
                _percentForFundamental = value;
            }
        }
    }
}